package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.AdminLogConverter;
import com.cskaoyan.wordmemorize.dao.entity.OperateLogDO;
import com.cskaoyan.wordmemorize.dao.mapper.OperateLogMapper;
import com.cskaoyan.wordmemorize.dto.admin.OperateLogDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.OperateLogPageRequest;
import com.cskaoyan.wordmemorize.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminGetLogOperationServiceImpl implements AdminLogService {

    @Autowired
    AdminLogConverter adminLogConverter;
    @Autowired
    OperateLogMapper operateLogMapper;

    // 分页查询系统操作日志(后台操作日志)(admin_operate_log)
    @Override
    public PageDTO<OperateLogDTO> getOperateLogPage(OperateLogPageRequest request) {
        Page<OperateLogDO> operateLogDOPage = new Page<>(request.getPageNum(), request.getCount());
        // 根据条件查询(员工id,日志数据类型)
        QueryWrapper<OperateLogDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(request.getEmployeeId() != null, "employee_id", request.getEmployeeId())
                .eq(request.getDataType() != null, "data_type", request.getDataType());
        List<OperateLogDO> records
                = operateLogMapper.selectPage(operateLogDOPage, queryWrapper).getRecords();
        long total = operateLogMapper.selectCount(queryWrapper);
        // 转换DTO
        PageDTO<OperateLogDTO> operateLogDTOPageDTO
                = adminLogConverter.operationLogPage2PageDTO(records, total);

        return operateLogDTOPageDTO;
    }
}
